package com.aaa.controller.back;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.aaa.entity.*;
import com.aaa.service.back.GoodsService;
import com.aaa.service.back.PurchaseService;
import com.aaa.service.back.SupplierService;
import com.aaa.service.back.WarehouseService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/purchase")
@Controller
public class PurchaseController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private SupplierService supplierService;
    @Resource
    private PurchaseService purchaseService;


    /**
     * 采购订单页面显示
     * @param model
     * @return
     */
    @RequestMapping("/purchaseShow")
    public String purchaseShow(Model model,Integer purchaseState){
        List<Map<String, Object>> purchaseList1 = purchaseService.findPurchaseByPurchaseState1();
        model.addAttribute("purchaseList1",purchaseList1);

        List<Map<String, Object>> purchaseList0 = purchaseService.findPurchaseByPurchaseState0(purchaseState);
        model.addAttribute("purchaseList0",purchaseList0);
        return "back/purchase-list";
    }

    /**
     * 已审核/未入库采购订单显示
     * @param model
     * @param purchaseState
     * @return
     */
    @RequestMapping("/purchaseShow0")
    public String purchaseShow0(Model model,Integer purchaseState){
        List<Map<String, Object>> purchaseList0 = purchaseService.findPurchaseByPurchaseState0(purchaseState);
        model.addAttribute("purchaseList0",purchaseList0);

        List<Map<String, Object>> purchaseList2 = purchaseService.findPurchaseByPurchaseState2(purchaseState);
        model.addAttribute("purchaseList2",purchaseList2);
        return "back/purchase_entry-list";
    }

    /**
     * 采购订单明细页面显示
     * @return
     */
    @RequestMapping("/purchaseDetailsShow")
    public String purchaseDetailsShow(Model model,String purchaseId){
        List<Map<String, Object>> purchaseDetailsList = purchaseService.findPurchaseDetails(purchaseId);
        model.addAttribute("purchaseDetailsList",purchaseDetailsList);
        return "back/purchaseDetails-list";
    }

    /**
     * 选择商品进行采购订单创建页面的显示
     * @param model
     * @return
     */
    @RequestMapping("/purchaseGoodsShow")
    public String purchaseGoodsShow(Model model){
        List<Map<String, Object>> goodsList = goodsService.findGoods();
        model.addAttribute("goodsList",goodsList);

        List<Map<String, Object>> warehouseList = warehouseService.findWarehouse();
        model.addAttribute("warehouseList",warehouseList);

        List<Map<String, Object>> supplierList = supplierService.findSupplier();
        model.addAttribute("supplierList",supplierList);
        return "back/purchaseGoods-list";
    }

    /**
     * 更新session购物车商品
     * @param session
     */
    public void updateCart(HttpSession session) {
        int cartCount= 0;
        double cartAmount=0;
        Map<Integer, Goods> cart = (Map<Integer, Goods>)session.getAttribute("purchase_cart");

        if (cart!=null) {
            for (Goods goods : cart.values()) {
                cartCount+=goods.getGoodsCount();
                cartAmount+=goods.getGoodsCount()*goods.getTradePrice();
            }

            session.setAttribute("cartCount", cartCount);
            session.setAttribute("cartAmount", cartAmount);
        }
    }

    /**
     * 添加商品到session购物车
     */
    @RequestMapping("/cartAdd")
    public String cartAdd(Integer gId,HttpSession session) {
        Map<Integer, Goods> cart = null;
        if (session.getAttribute("purchase_cart")==null) {
            cart = new HashMap<Integer, Goods>();
            session.setAttribute("purchase_cart", cart);
        } else {
            cart = (Map<Integer, Goods>)session.getAttribute("purchase_cart");
        }

        Goods goods = cart.get(gId);

        if (goods==null) {
            goods = (Goods) goodsService.findGoodsByGoodsId(gId);
            goods.setGoodsCount(1);
            cart.put(gId, goods);
        } else {
            goods.setGoodsCount(goods.getGoodsCount()+1);
        }
        updateCart(session);
        return "redirect:/purchase/purchaseGoodsShow";
    }

    /**
     * 修改session购物车的商品数量
     * @param gId
     * @param op
     * @param session
     * @return
     */
    @RequestMapping("/cart_process")
    public String cart_process(Integer gId,String op,HttpSession session){

        Map<Integer,Goods> cart = (Map<Integer,Goods>)session.getAttribute("purchase_cart");

        if(cart!=null){
            Goods goods = cart.get(gId);
            if("add".equals(op)){
                goods.setGoodsCount(goods.getGoodsCount()+1);
            }else if("sub".equals(op)){
                //减数量时，如果商品只有1个，则直接移除
                if(goods.getGoodsCount()==1){
                    cart.remove(gId);
                }else{
                    goods.setGoodsCount(goods.getGoodsCount()-1);
                }
            }else if("delete".equals(op)){
                cart.remove(gId);
            }
            //更新购物车数量和金额
            updateCart(session);
        }
        return "redirect:/purchase/purchaseGoodsShow";
    }

    /**
     * 进行采购订单创建
     * @return
     */
    @RequestMapping("/purchaseSubmit")
    public String purchaseSubmit(HttpSession session, Purchase purchase){
//        Emp emp = (Emp) session.getAttribute("empInfo");
//        Emp emp1 = new Emp();
//        emp1.setEmpId(emp.getEmpId());
//        purchase.setEmp(emp1);
        Date date = new Date();
        purchase.setBusinessDate(date);
        purchase.setPurchaseState(0);
        Double cartAmount = (Double) session.getAttribute("cartAmount");
        purchase.setAmountPayable(cartAmount);
        purchaseService.addPurchase(purchase);

        String purchaseMAX = purchaseService.findPurchaseByMAX();

        Map<Integer,Goods> cart = (Map<Integer,Goods>)session.getAttribute("purchase_cart");
        PurchaseDetails purchaseDetails = new PurchaseDetails();

        Purchase purchase1 = new Purchase();
        purchase1.setPurchaseId(purchaseMAX);
        purchaseDetails.setPurchase(purchase1);


        for (Goods goods : cart.values()){

            Goods goods1 = new Goods();
            goods1.setGoodsId(goods.getGoodsId());
            purchaseDetails.setGoods(goods);

            purchaseDetails.setGoodsCount(goods.getGoodsCount());
            purchaseDetails.setGoodsPrice(goods.getTradePrice());
            purchaseService.addPurchaseDetails(purchaseDetails);
        }
        session.removeAttribute("purchase_cart");
        session.removeAttribute("cartAmount");

        return "redirect:/purchase/purchaseShow";
    }

    /**
     * 审核采购订单，修改采购订单状态为已审核
     * @param purchase
     * @return
     */
    @RequestMapping("/editPurchase")
    public String editPurchase(Purchase purchase){
        purchaseService.editPurchase(purchase);
        return "redirect:/purchase/purchaseShow";
    }

    /**
     * 修改采购订单状态未入库为已入库，并且成功入库
     * @return
     */
    @RequestMapping("/editPurchase0Submit")
    public String editPurchase0(String purchaseId){

        purchaseService.editPurchase0(purchaseId);

        Purchase purchase = purchaseService.findByPurchaseId(purchaseId);


        List<PurchaseDetails> byPurchaseDetailsList = purchaseService.findByPurchaseDetails(purchaseId);

        for (PurchaseDetails purchaseDetails : byPurchaseDetailsList){

            WarehouseDetails wd = warehouseService.FindWarehouseDetailsByGoodsIdAndWarehouseId(purchaseDetails.getGoods().getGoodsId(), purchase.getWarehouse().getWarehouseId());

            System.out.println(purchaseDetails.getGoods().getGoodsId());
            System.out.println(purchase.getWarehouse().getWarehouseId());

            if (wd==null){
                WarehouseDetails warehouseDetails = new WarehouseDetails();

                warehouseDetails.setWarehouse(purchase.getWarehouse());
                warehouseDetails.setGoodsCount(purchaseDetails.getGoodsCount());
                warehouseDetails.setGoods(purchaseDetails.getGoods());
                warehouseService.addWarehouseDetails(warehouseDetails);
            }else {
                warehouseService.editWarehouseDetails(purchaseDetails.getGoodsCount(),purchaseDetails.getGoods().getGoodsId(),purchase.getWarehouse().getWarehouseId());
            }
        }
        return "redirect:/purchase/purchaseShow0";
    }
}
